package com.wishwingz.homeworkchat;

import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.actionbarsherlock.app.SherlockFragment;
import com.wishwingz.homeworkchat.XMPPManager.OnLoginListener;

public class MoreTabFragment extends SherlockFragment {

	private View view;
	private EditText userText;
	private EditText pwText;
	private Button btn;
	private TextView tx;
	private String username;
	private String password;
	private SharedPreferences sPref;
	static final String SHARED_PREF_USERNAME = "username";

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		sPref = getActivity().getSharedPreferences(
				MainActivity.SHARED_PREF_LOGINDATA, MainActivity.MODE_PRIVATE);

		boolean loginYN = sPref.getBoolean(MainActivity.SHARED_PREF_LOGINYN,
				false);

		if (loginYN) {
			view = inflater.inflate(R.layout.more_fragment_true, container,
					false);

			tx = (TextView) view.findViewById(R.id.loggin_username);
			tx.setText(sPref.getString(SHARED_PREF_USERNAME, ""));

			btn = (Button) view.findViewById(R.id.logout_btn);
			btn.setOnClickListener(new View.OnClickListener() {

				@Override
				public void onClick(View v) {
					// TODO Auto-generated method stub
					XMPPManager.getInstance().logout();
					SharedPreferences.Editor editor = sPref.edit();
					editor.putBoolean(MainActivity.SHARED_PREF_LOGINYN, false);
					editor.putString(SHARED_PREF_USERNAME, null);
					editor.commit();
					MoreTabFragment.this.onResume();
				}
			});

		} else {
			view = inflater.inflate(R.layout.more_fragment_false, container,
					false);
			userText = (EditText) view.findViewById(R.id.username);
			pwText = (EditText) view.findViewById(R.id.password);

			btn = (Button) view.findViewById(R.id.login_btn);
			btn.setOnClickListener(new View.OnClickListener() {

				@Override
				public void onClick(View v) {
					// TODO Auto-generated method stub

					username = userText.getText().toString();
					password = pwText.getText().toString();

					if (username.equals("") || username == null) {
						Toast.makeText(getActivity(), "Please enter user name",
								Toast.LENGTH_SHORT).show();
						return;
					}

					Toast.makeText(getActivity(), username, Toast.LENGTH_SHORT)
							.show();
					Toast.makeText(getActivity(), password, Toast.LENGTH_SHORT)
							.show();

					XMPPManager.getInstance().login(username, password,
							new XMPPManager.OnLoginListener() {

								@Override
								public void onLoginSuccess(String username) {
									// TODO Auto-generated method stub
									Toast.makeText(getActivity(),
											"Login success", Toast.LENGTH_SHORT)
											.show();
									SharedPreferences.Editor editor = sPref
											.edit();
									editor.putBoolean(
											MainActivity.SHARED_PREF_LOGINYN,
											true);
									editor.putString(SHARED_PREF_USERNAME,
											username);
									editor.commit();
									MoreTabFragment.this.onResume();
								}

								@Override
								public void onLoginFail(String username) {
									// TODO Auto-generated method stub
									Toast.makeText(getActivity(),
											"Login failed, please try again",
											Toast.LENGTH_SHORT).show();
								}
							});

				}
			});

		}

		return view;
	}
}